<template>
  <div class="alter">
    <!-- 修改密码 -->
    <el-row>
      <el-col :span='24' >
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="alter-ruleForm"
        >
          <el-form-item label="原密码" prop="oldpass">
            <el-input type="password" v-model="ruleForm.oldpass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import jwt_decode from 'jwt-decode'
export default {
    data() {
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          pass: '',
          checkPass: '',
          oldpass:'',
        },
        
        rules: {
           oldpass: [
            {required:true,message:'密码不能为空', trigger: 'blur' },
          ],
          pass: [
            {required:true,message:'密码不能为空', trigger: 'blur' },
            {min:6,max:6,message:'密码必须是6位',trigger:'blur'}
          ],
           checkPass: [
            {required:true,message:'确认密码不能为空', trigger: 'blur' },
             { validator: validatePass2, trigger: 'blur' }
          ],
        
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            var phone=jwt_decode(localStorage.getItem('mytoken')).phone||jwt_decode(sessionStorage.getItem('mytoken'))
            this.$axios.put('http://localhost:3000/users/changePsw/'+phone,this.ruleForm)
            .then(res=>{
                console.log(res.data)
                if(res.data.data==-1){
                    this.$message('密码错误，请重新输入')
                }else{
                    this.$message("密码更改成功")
                }
            })
            .catch(err=>{
                console.log(err)
            })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }

</script>
<style lang='css' scoped>
.alter-ruleForm{
  width: 400px;
  height: 380px;
  padding:20px 50px 10px 10px;
  margin:0 auto
}
</style>